package com.easoft.code.pos.util;

import com.easoft.code.pos.data.dao.GenericDao;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.poi.util.IOUtils;

/**
 *
 * @author Eliu
 */
public class Valores {

    private static Properties caeProperties;
    private static Properties posProperties;

    public static final String DATE_FORMAT = "sys.dateformat";
    public static final String TIME_FORMAT = "sys.dateformat";
    public static final String WIN_HEIGHT = "sys.win_height";
    public static final String WIN_WIDTH = "sys.win_width";

    public static final String POS_DEF_OPER = "pos.doc_oper_tipo";

    public static final String DB_URL = "db.url";
    public static final String DB_USER = "db.user";
    public static final String DB_PASSWORD = "db.password";

    public static Integer OPER_APLICADO = 1;
    public static Integer OPER_ANULADO = 0;
    public static Integer OPER_NOAPLICADO = 2;

    public static Integer PRECIO_POS = 1;
    public static Integer PRECIO_NORMAL = 2;
    public static Integer PRECIO_RECARGO_PORC = 1;
    public static Integer PRECIO_RECARGO_FIJO = 2;

    /**
     *
     * @param key
     * @return
     */
    public static String getCAEConfigValue(String key) {

        if (caeProperties == null) {
            try {
                caeProperties = new Properties();
                InputStream r = Valores.class.getResourceAsStream("/CAEConfiguration.properties");
                caeProperties.load(r);
            } catch (IOException ex) {
                Logger.getLogger(Valores.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return caeProperties.getProperty(key);
    }

    public static Properties loadConfiguration() throws IOException {
        String conFolder = "/ea/conf/";
        String conFile = "easoftpos.properties";
        File folder = new File(conFolder);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        final File cfgFile = new File(conFolder + conFile);
        if (!cfgFile.exists()) {
            try {
                cfgFile.createNewFile();
                InputStream inPackedCfgFile = GenericDao.class.getResourceAsStream("/" + conFile);
                OutputStream cfgFileOut = FileUtils.openOutputStream(cfgFile);
                IOUtils.copy(inPackedCfgFile, cfgFileOut);
                IOUtils.closeQuietly(cfgFileOut);
                IOUtils.closeQuietly(inPackedCfgFile);

            } catch (IOException ex) {
                ex.printStackTrace(System.err);
            }
        }
        Properties props = new Properties();
        props.load(new FileInputStream(cfgFile));
        return props;
    }

    /**
     *
     * @param key
     * @return
     */
    public static String getConfigValue(String key) {
        if (posProperties == null) {
            try {
                posProperties = loadConfiguration();
            } catch (IOException ex) {
                Logger.getLogger(Valores.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return posProperties.getProperty(key);
    }

}
